﻿#

param
(
	[string]$endpoints , # 'https://192.168.168.1:2379,https://192.168.168.2:2379' 后面不要有/

	[string]$pfx
)

if (Test-Path -LiteralPath $pfx)
{
}
else
{
	Write-Error '错误：找不到pfx文件。返回码 1'
	exit 1
}

if ( ($null -eq $endpoints) -or ($endpoints -eq '') )
{
	if ( ($null -eq $global:endpoints) -or ($global:endpoints -eq '') )
	{
		Write-Error '错误：endpoints值错误。返回码 21'
		exit 21
	}
	else
	{
		$endpoints = $global:endpoints
	}
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#问：这个脚本谁写的？有问题找谁技术支持?
#答：QQ群号=722528388
#名称=k4t国产k8s群1
#华之夏，脚之巅，有我ps1片天！
#专门教学win，linux通用的ps1脚本。不想学也可以，入群用红包求写脚本。

$private:params_memberlist = @{
	ContentType = 'application/x-www-form-urlencoded'
	Method      = 'POST'
	URI         = "${endpoints}/v3/cluster/member/list"
	TimeoutSec  = 4
	Certificate = Get-PfxCertificate -FilePath $pfx
}

$private:params_endpointsstates = @{
	ContentType = 'application/x-www-form-urlencoded'
	Method      = 'POST'
	URI         = "${endpoints}/v3/maintenance/status"
	TimeoutSec  = 4
	Certificate = Get-PfxCertificate -FilePath $pfx
}

$private:rl = Invoke-WebRequest @private:params_memberlist -SkipCertificateCheck
$private:rs = Invoke-WebRequest @private:params_endpointsstates -SkipCertificateCheck

$private:rl2 = $rl | ConvertFrom-Json
$private:rs2 = $rs.Content | ConvertFrom-Json

$rt = [pscustomobject]@{
	节点id       = $private:rs2.header.member_id
	节点名        = $private:rl2.members.name
	version    = $private:rs2.version
	leader     = & {
		if ($private:rs2.header.member_id -eq $private:rs2.leader)
		{
			return $true
		}
		else
		{
			return $false
		}
	}
	peerURLs   = $private:rl2.members.peerURLs
	clientURLs = $private:rl2.members.clientURLs
	健康         = & "${PSScriptRoot}/get-k8sHealth.ps1" -endpoints $endpoints -pfx $pfx
}

$private:gs = `
@{Expression = { $_.节点id };Label = "节点id";width = 28 },
@{Expression = { $_.节点名 };Label = "节点名";width = 16 },
@{Expression = { $_.健康 };Label = "健康" ;width = 6 },
@{Expression = { $_.version };Label = "version";width = 7 },
@{Expression = { $_.leader };Label = "leader";width = 6 },
@{Expression = { $_.peerURLs };Label = "peerURLs";width = 28 },
@{Expression = { $_.clientURLs };Label = "clientURLs";width = 28 }

$rt | Format-Table $private:gs -Wrap

exit 0

